﻿using Bricks.Business.Models.Account;
using Bricks.Component.Utils;
using Bricks.Business.Data.Repositories.Account;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using Bricks.Business.Data.Repositories.Systems;
using Bricks.Business.Models.Systems;
using Bricks.Site.Models.DBViewModel.Dictionary;
using Bricks.Component.Data;

namespace Bricks.Business.Service.Impl
{
    /// <summary>
    /// 功能管理
    /// </summary>
    [Export(typeof(IDictionaryService))]
    public class DictionaryService : IDictionaryService
    {
        [Import]
        protected IDictionaryRepository DictionaryRepository { get; set; }

        [Import]
        protected IDictionaryTypeRepository DictionaryTypeRepository { get; set; }

        public IQueryable<DictionaryType> DictionaryTypes
        {
            get { return DictionaryTypeRepository.Entities; }
        }

        public IQueryable<Dictionary> Dictionarys
        {
            get { return DictionaryRepository.Entities; }
        }

        /// <summary>
        /// 根据字典类型Tag获取字典列表
        /// </summary>
        /// <param name="typeTag"></param>
        /// <returns></returns>
        public List<Dictionary> GetDictionaryByTypeTag(string typeTag)
        {
            List<Dictionary> result = new List<Dictionary>();
            var list = from d in Dictionarys
                       join dt in DictionaryTypes on d.DictionaryTypeID equals dt.ID
                       where dt.Tag == typeTag
                       orderby dt.OrderNo
                       select d;
            result = list.OrderBy(m => m.OrderNo).ToList();
            return result;
        }

        #region 字典类型
        /// <summary>
        /// 新增字典类型
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public OperationResult AddType(DictionaryType model)
        {
            DictionaryType record = model.CopyTo<DictionaryType>();
            record.ID = GuidHelper.NewGuid();
            record.OrderNo = DictionaryTypeRepository.GetNextOrderNo(m => m.OrderNo);  //获取下一个排序号
            DictionaryTypeRepository.Insert(record);
            return new OperationResult(OperationResultType.Success, "新增成功", record);
        }

        /// <summary>
        /// 修改字典类型
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public OperationResult ModifyType(DictionaryType model)
        {
            PublicHelper.CheckArgument(model, "model");
            DictionaryType record = DictionaryTypes.FirstOrDefault(m => m.ID == model.ID);
            if (record == null)
                return new OperationResult(OperationResultType.QueryNull, "未找到指定的记录");

            record = model.CopyTo<DictionaryType>();
            if (DictionaryTypeRepository.Update(record) > 0)
                return new OperationResult(OperationResultType.Success, "修改成功", record);
            return new OperationResult(OperationResultType.Error, "保存失败");
        }

        /// <summary>
        /// 删除字典类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public OperationResult DeleteType(Guid id)
        {
            DictionaryType record = DictionaryTypes.FirstOrDefault(m => m.ID == id);
            if (record == null)
                return new OperationResult(OperationResultType.QueryNull, "未找到指定的记录");

            if (DictionaryTypeRepository.Delete(record) >= 0)
                return new OperationResult(OperationResultType.Success, "删除成功", record);
            return new OperationResult(OperationResultType.Error, "删除失败");
        }

        /// <summary>
        /// 排序设置字典类型(向上向下)
        /// </summary>
        /// <param name="id"></param>
        /// <param name="pid"></param>
        /// <param name="upOrDown"></param>
        /// <returns></returns>
        public OperationResult SetOrderType(Guid id, string upOrDown)
        {
            IQueryable<DictionaryType> entities = DictionaryTypeRepository.Entities;  //找出可能涉及排序的实体集合（同一PID下）
            var exeResult = DictionaryTypeRepository.SetOrder(entities, id, "OrderNo", upOrDown);  //执行向上向下操作
            return exeResult;
        }
        #endregion

        #region 字典
        /// <summary>
        /// 新增字典
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public OperationResult AddDictionary(Dictionary model)
        {
            Dictionary record = model.CopyTo<Dictionary>();
            record.ID = GuidHelper.NewGuid();
            record.OrderNo = DictionaryRepository.GetNextOrderNo(m => m.OrderNo);  //获取下一个排序号
            DictionaryRepository.Insert(record);
            return new OperationResult(OperationResultType.Success, "新增成功", record);
        }

        /// <summary>
        /// 修改字典
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public OperationResult ModifyDictionary(Dictionary model)
        {
            PublicHelper.CheckArgument(model, "model");
            Dictionary record = Dictionarys.FirstOrDefault(m => m.ID == model.ID);
            if (record == null)
                return new OperationResult(OperationResultType.QueryNull, "未找到指定的记录");

            record = model.CopyTo<Dictionary>();
            if (DictionaryRepository.Update(record) > 0)
                return new OperationResult(OperationResultType.Success, "修改成功", record);
            return new OperationResult(OperationResultType.Error, "保存失败");
        }

        /// <summary>
        /// 删除字典
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public OperationResult DeleteDictionary(Guid id)
        {
            Dictionary record = Dictionarys.FirstOrDefault(m => m.ID == id);
            if (record == null)
                return new OperationResult(OperationResultType.QueryNull, "未找到指定的记录");

            if (DictionaryRepository.Delete(record) >= 0)
                return new OperationResult(OperationResultType.Success, "删除成功", record);
            return new OperationResult(OperationResultType.Error, "删除失败");
        }

        /// <summary>
        /// 排序设置字典(向上向下)
        /// </summary>
        /// <param name="id"></param>
        /// <param name="pid"></param>
        /// <param name="upOrDown"></param>
        /// <returns></returns>
        public OperationResult SetOrderDictionary(Guid id, string upOrDown)
        {
            IQueryable<Dictionary> entities = DictionaryRepository.Entities;  //找出可能涉及排序的实体集合（同一PID下）
            var exeResult = DictionaryRepository.SetOrder(entities, id, "OrderNo", upOrDown);  //执行向上向下操作
            return exeResult;
        }
        #endregion

        public List<VMDictionaryInfo> GetVMDictionaryInfo()
        {
            var list = DbSqlHelper<VMDictionaryInfo>.SqlQueryCommon("select * from V_DictionaryInfo");
            return list;
        }
    }
}
